define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'yalimedia/lecture/index',
                    multi_url: 'yalimedia/lecture/multi',
                    table: 'yalimedia_lecture',
                    add_url: 'yalimedia/lecture/add',
                    edit_url: 'yalimedia/lecture/edit',
                    del_url: 'yalimedia/lecture/del',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                sortOrder:'desc',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate:false},
                        {field: 'name', title: __('讲座名称'),operate:'like'},
                        {field: 'type', title: __('讲座类型'),operate:false},
                        {field: 'info', title: __('直播/位置信息'),operate:'like'},
                        {field: 'createtime', title: __('添加时间'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    visible:function (row) {
                                        if(row['status'] == "0"){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                    name: 'check',
                                    title: __('内容审核'),
                                    classname: 'btn btn-xs btn-success btn-danger btn-ajax',
                                    icon: 'fa fa-exclamation-circle',
                                    confirm: '确认审核通过吗，不可撤销？',
                                    url: 'yalimedia/lecture/check',
                                    success: function (data, ret) {
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                        table.bootstrapTable('refresh')
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },{
                                    name: 'allocation',
                                    title: __('分给某班'),
                                    text:'听课班级',
                                    extend:'data-area=["30%","100%"]',
                                    classname: 'btn btn-xs btn-default btn-dialog',
                                    icon: 'fa fa-bars',
                                    url: 'yalimedia/lecture/allocation'
                                }],
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        allocation: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});